<template>
  <view class="page">
    <view class="search-bar">
      <view class="search-input">
        <input type="text" v-model="keywords" placeholder="请输入标题/短信内容" />
        <image class="icon-search" src="/static/images/icon_search.png" mode=""></image>
        <view class="search-button" @click="search">搜索</view>
      </view>
    </view>
    <u-dropdown class="dropdown" :activeColor="activeColor" :borderBottom="borderBottom" :close-on-click-mask="mask">
      <u-dropdown-item v-model="remindType" :title="remindTypeTitle" :options="remindTypeOption">
        <view class="dropdown-content">
          <view class="type-list">
            <view v-for="(item, index) in remindTypeOption" :key="index" class="type-item"
              :class="[item.active ? 'active' : '']" @tap="tagClick(index)">
              {{item.label}}
            </view>
          </view>
          <view class="dropdown-buttons">
            <view class="dropdown-button primary" @click="remindTypeChange">保存</view>
            <view class="dropdown-button" @click="remindTypeClear">清空</view>
          </view>
        </view>
        <!-- <view class="slot-content">
          <view class="item-box">
            <view class="item" :class="[item.active ? 'active' : '']" @tap="tagClick(index)"
              v-for="(item, index) in remindTypeOption" :key="index">
              {{item.label}}
            </view>
          </view>
          <u-row gutter="16">
            <u-col span="6">
              <u-button type="warning" @click="remindTypeClear">清空</u-button>
            </u-col>
            <u-col span="6">
              <u-button type="primary" @click="remindTypeChange">确定</u-button>
            </u-col>
          </u-row>
        </view> -->
      </u-dropdown-item>
      <u-dropdown-item @change="statusChange" v-model="status" :title="statusTitle"
        :options="statusOption"></u-dropdown-item>
    </u-dropdown>
    <scroll-view class="scroll-list" scroll-y="true" @scrolltolower="loadMore">
      <view class="message-list">
        <view v-for="(item, index) in list" :key="index" class="message-item" @click="handleClick(item)">
          <view class="message-item-bd">
            <view class="message-title">{{item.title}}</view>
            <view class="message-desc">{{item.createTime}}</view>
            <view class="message-status" :class="{disabled: item.status}">{{item.statusName}}</view>
          </view>
        </view>
      </view>
      <!-- <view class="uni-list" v-for="(item, index) in list" :key="index" style="margin-top: 5rpx;margin-bottom: 5rpx;"
        @click="handleClick(item)">
        <u-row gutter="16" style="margin-top: 20rpx;">
          <u-col span="1"
            style="word-break:break-all;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden;">
            <u-icon v-if="getIconName(item)!=''" style="margin-right: 10rpx;" :name="getIconName(item)" color="#2b85e4"
              size="50"></u-icon>
            <i v-else class="fjutfont fjut-jiejiaripeizhi" style="color:#2b85e4;font-size: 40rpx;"></i>
          </u-col>
          <u-col span="8"
            style="word-break:break-all;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden;">
            {{item.title}}
          </u-col>
          <u-col span="3" style="text-align: right;">
            <view v-if="item.status==0">
              <font style="border:1px solid green; color: green;">{{item.statusName}}</font>
            </view>
            <view v-else style="color:red;">
              <text style="border:1px solid red;">{{item.statusName}}</text>
            </view>
          </u-col>
        </u-row>
        <u-row gutter="16" style="margin-top: 20rpx;">
          <u-col span="1">

          </u-col>
          <u-col span="11">
            <view style="text-align: left;color: gray;">
              {{item.createTime}}
            </view>
          </u-col>
        </u-row>
      </view> -->
      <u-loadmore class="loadmore" :status="loadStatus"></u-loadmore>
    </scroll-view>
    <view v-if="hasPermission('msgHolidyMsg_add')" class="add-btn" @click="navTo('form')">
      <image src="/static/images/icon_add_message.png" mode=""></image>
    </view>
  </view>
</template>
<script>
  import $time from '@/common/time.js';
  import JsCollapseItem from '@/components/js-collapse-item/js-collapse-item.vue'
  export default {
    data() {
      return {
        keywords: '',
        status: '',
        remindType: '',
        query: {
          current: 1,
          size: 10
        },
        swiperCurrent: 0,
        list: [],
        pages: 0,
        loadStatus: 'loadmore',
        options: [],
        headStyle: {
          fontWeight: 'bold',
          color: '#606266',
          margin: '16rpx'
        },
        borderBottom: false,
        activeColor: '#3989FC',
        mask: true,
        remindTypeOption: [],
        statusOption: [],
        remindTypeTitle: '提醒类型',
        statusTitle: '状态'
      };
    },
    onLoad() {
      this.loadList();
      this.initRemindType();
      this.initStatus();
    },
    computed: {

    },
    methods: {
      navTo(url) {
        uni.navigateTo({
          url: url
        });
      },
      search(value) {
        this.list = [];
        this.query.current = 0;
        this.loadList();
      },

      loadList() {
        this.query.keywords = this.keywords;
        this.$u.api.message.msgHolidyMsg.list(this.query).then(res => {
          if (!res.data.records || res.data.records.length == 0) {
            this.loadStatus = "nomore";
            return;
          }
          res.data.records.forEach((item) => {
            item.show = false;
          })
          this.list = this.list.concat(res.data.records);
          this.pages = res.pages;
          this.query.current = res.data.current;
          this.query.size = res.data.size;
          this.loadStatus = "loadmore";
        });
      },
      loadMore() {
        this.loadStatus = "loading";
        setTimeout(() => {
          this.query.current += 1;
          this.loadList();
        }, 100);
      },
      getContent(value) {
        let res = value.replace(/<.+?>/g, '').replace(/&nbsp;/ig, '').replace(/\s/ig, '');
        return res;
      },
      getIconName(row) {
        if (row.remindType === '1') {
          return "gift";
        } else if (row.remindType === '4') {
          return "heart";
        } else {
          return "";
        }
      },
      handleDelete(row) {
        let self = this;
        uni.showModal({
          title: '提示',
          content: '确认要删除该数据吗,删除后无法恢复？',
          showCancel: true,
          success: function(res2) {
            if (res2.confirm) {
              self.$u.api.message.msgHolidyMsg.delete({
                ids: row.id
              }).then(res => {
                self.$u.toast('删除成功！');
                self.search();
              });
            }
          }
        });
      },
      initRemindType() {
        this.$u.api.dictBizData({
          code: 'remindType'
        }).then(res => {
          let detaiList = [];
          this.releaseType = res.data;
          res.data.forEach((item) => {
            detaiList.push({
              'label': item.dictValue,
              'value': item.dictKey,
              'active': false
            });
          })
          this.remindTypeOption = detaiList;
        });
      },
      initStatus() {
        this.$u.api.dictBizData({
          code: 'status_type'
        }).then(res => {
          let detaiList = [];
          let detailJson = {
            'label': '全部',
            'value': ''
          }
          detaiList.push(detailJson)
          this.releaseType = res.data;
          res.data.forEach((item) => {
            detaiList.push({
              'label': item.dictValue,
              'value': item.dictKey
            });
          })
          this.statusOption = detaiList;
        });
      },
      remindTypeChange(index) {
        this.list = [];
        this.query.current = 0;
        let val = this.getActiveVal(this.remindTypeOption);
        if (val.length > 0) {
          this.query.remindTypes = val.join(",");
          this.remindTypeTitle = this.getOptionLabel(this.remindTypeOption, val[0]);
          if (val.length > 1) {
            this.remindTypeTitle = this.remindTypeTitle + '...';
          }
        } else {
          this.query.remindTypes = '';
          this.remindTypeTitle = "提醒类型";
        }
        this.$refs.uDropdown.close();
        this.loadList()
      },
      statusChange(index) {
        this.list = [];
        this.query.current = 0;
        this.query.status = index
        this.statusTitle = (index === '' ? '状态类型' : this.getOptionLabel(this.statusOption, index));
        this.loadList()
      },
      handleClick(row) {
        if (this.hasPermission('msgHolidyMsg_view')) {
          this.navTo('form?id=' + row.id);
        }
      },
      tagClick(index) {
        this.remindTypeOption[index].active = !this.remindTypeOption[index].active;
      },
      remindTypeClear() {
        this.remindTypeOption.forEach((item) => {
          item.active = false;
        })
      }
    }
  };
</script>
<style lang="scss">
  .page {
    position: relative;
    background: #F7F7F7;
  }

  .search-bar {
    display: flex;
    align-items: center;
    padding: 16rpx 32rpx;
    background: #fff;
  }

  .search-input {
    position: relative;
    width: 100%;
    height: 64rpx;
    border-radius: 32rpx;
    background: #F7F7F7;
    overflow: hidden;

    input {
      width: 100%;
      height: 64rpx;
      border: 0;
      padding: 0 144rpx 0 64rpx;
      outline: 0;
      line-height: 64rpx;
      font-size: 24rpx;
      font-weight: 500;
      color: #999;
      background: transparent;
    }

    .icon-search {
      position: absolute;
      top: 21rpx;
      left: 21rpx;
      width: 22rpx;
      height: 22rpx;
    }

    .search-button {
      position: absolute;
      top: 0;
      right: 0;
      width: 144rpx;
      border-radius: 32rpx;
      line-height: 64rpx;
      text-align: center;
      font-size: 24rpx;
      font-weight: 700;
      color: #fff;
      background: #3989FC;
    }
  }

  .dropdown {
    height: 80rpx;
    background: #fff;
  }
  
  .dropdown-content {
    border-top: 1px solid #f7f7f7;
    padding: 24rpx;
    background: #fff;
  }
  
  .dropdown-buttons {
    display: flex;
  }
  
  .dropdown-button {
    width: 100%;
    height: 64rpx;
    border: 1px solid #dedede;
    border-radius: 64rpx;
    line-height: 62rpx;
    text-align: center;
    font-size: 28rpx;
    font-weight: 500;
    color: #999;
    background: #fff;
  
    &.primary {
      border: 1px solid #3989FC;
      color: #fff;
      background: #3989FC;
    }
  
    &+.dropdown-button {
      margin-left: 24rpx;
    }
  }

  .type-list {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }

  .type-item {
    display: inline-block;
    border: 1px solid #3989FC;
    border-radius: 48rpx;
    padding: 0 32rpx;
    margin-bottom: 24rpx;
    line-height: 48rpx;
    font-size: 28rpx;
    color: #3989FC;

    &.active {
      color: #fff;
      background: #3989FC;
    }
  }

  .scroll-list {
    height: calc(100vh - var(--window-top) - var(--window-bottom) - 96rpx - 88rpx);

    .loadmore {
      padding: 32rpx;
    }
  }

  .message-list {
    display: flex;
    flex-direction: column;
    padding: 32rpx 24rpx 0;
  }

  .message-item {
    width: 100%;
    border-radius: 8rpx;
    background: #fff;

    &+.message-item {
      margin-top: 32rpx;
    }
  }

  .message-item-bd {
    position: relative;
    padding: 32rpx;
  }

  .message-title {
    padding-right: 96rpx;
    margin-bottom: 16rpx;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-weight: 700;
    font-size: 32rpx;
    color: #333;
    overflow: hidden;
  }

  .message-desc {
    font-size: 24rpx;
    font-weight: 500;
    color: #999;
  }

  .message-status {
    position: absolute;
    top: 32rpx;
    right: 32rpx;
    display: inline-block;
    border-radius: 24rpx;
    padding: 0 24rpx;
    line-height: 48rpx;
    text-align: center;
    font-size: 24rpx;
    font-weight: 500;
    color: #3989FC;
    background: #DEEBFE;

    &.disabled {
      color: #FF6600;
      background: rgba(255, 102, 0, .15);
    }
  }

  .add-btn {
    position: absolute;
    right: 32rpx;
    bottom: 288rpx;
    z-index: 99;
    width: 88rpx;
    height: 88rpx;

    image {
      width: 88rpx;
      height: 88rpx;
      vertical-align: top;
    }
  }
</style>